package coe.pumbaa
import grails.plugins.springsecurity.Secured

class HomeController {

    def stringManagerService
    def springSecurityService
    def authenticationManagerService
    
    def index() {
        def posts                
        if(springSecurityService.isLoggedIn()){
            redirect(action:'home',params:params)
        }else{
            def incommingEvents = getIncommingList()
            posts = authenticationManagerService.getPostList(authenticationManagerService.getAnonymousUser())
            [postInstanceList:posts,navigation:[pos:'home'], eventInstanceList: incommingEvents]
        }
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def home(){
        def user = springSecurityService.currentUser
        def incommingEvents = getIncommingList()
        def postSubList = authenticationManagerService.getPostSubList(user,params)
        def postList = authenticationManagerService.getPostList(user)
        [eventInstanceList: incommingEvents, postSubList:postSubList,postList:postList,navigation:[pos:'home']]
    }
    private def getIncommingList(){
        def incommingEvents =  Event.withCriteria(max: 10) {
            def now = new Date()            
            or{
                gt('startTime', now - 1)
                gt('endTime', now - 1)
            }
            order("startTime", "asc")
            order("endTime", "asc")
        }
        return incommingEvents
    }
}
